package cn.frh.log.aspect;

import cn.frh.log.annotation.Log;
import cn.frh.log.domain.LogTable;
import cn.frh.log.service.ILogTableService;
import cn.frh.log.util.RequestUtils;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;

import javax.security.auth.login.LoginContext;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

@Aspect
@Component
public class LogAspect {

    private Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Autowired
    private ILogTableService logTableService;

    @Autowired
    ObjectMapper objectMapper;

    private ThreadLocal<Date> startTime = new ThreadLocal<Date>();

    @Pointcut("@annotation(cn.frh.log.annotation.Log)")
    public void pt(){}

    @Around("pt()")
    public Object log(ProceedingJoinPoint joinPoint) throws Exception{
        // 获得注解
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        Log log = method.getAnnotation(Log.class);

        // 操作数据库日志表
        LogTable logTable = new LogTable();
        logTable.setRemark(log.value());
        // 请求信息
        HttpServletRequest request = RequestUtils.getRequest();
        //Logininfo loginUser = LoginContext.getLoginUser(request);
        //logTable.setOperator(loginUser.getUsername());
        logTable.setOperateDate(new Date());
        logTable.setHost(request.getRemoteHost());
        logTable.setUri(request.getRequestURI().toString());
    //  erpLog.setHeader(request.getHeader(HttpHeaders.USER_AGENT));
        logTable.setHttpMethod(request.getMethod());
        // 请求的方法参数值
        Object proceed = null;
        try {
            proceed = joinPoint.proceed();
            logTable.setResult("成功");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println(throwable.getMessage());
            logTable.setResult("失败");
        }
        logTableService.add(logTable);
        return proceed;
    }

    /**
     * 是否存在注解，如果存在就获取
     */
    private Log getAnnotationLog(Method method) {
        if (method != null) {
            return method.getAnnotation(Log.class);
        }
        return null;
    }

    private Method getMethod(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method != null) {
            return method;
        }
        return null;
    }
}